package org.filetransfer.file_transfer_java_backend;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import java.time.LocalDate;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@SpringBootTest
@AutoConfigureMockMvc
public class StatControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void testGetTypeStats() throws Exception {
        mockMvc.perform(get("/api/stat/visit"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.today").exists())
                .andExpect(jsonPath("$.total").exists());
    }

    @Test
    public void testGetAllTypeStats() throws Exception {
        mockMvc.perform(get("/api/stat/overview"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.visit").exists())
                .andExpect(jsonPath("$.visit.today").exists())
                .andExpect(jsonPath("$.visit.total").exists());
    }

    @Test
    public void testGetRangeStats() throws Exception {
        String type = "visit";
        LocalDate start = LocalDate.now().minusDays(6);
        LocalDate end = LocalDate.now();

        mockMvc.perform(get("/api/stat/" + type + "/range")
                        .param("startDate", start.toString())
                        .param("endDate", end.toString())
                        .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$").isMap()); // map<date, count>
    }
}
